package com.yuanye.yeecho.base;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.yuanye.yeecho.R;

public class CommonTaskFragment extends BaseFragment {

    protected TextView textView;
    protected ScrollView scrollView;
    protected Context context;

    public CommonTaskFragment() {}


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = requireContext();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag_common, container, false);
        textView = view.findViewById(R.id.textView);
        scrollView = view.findViewById(R.id.scroll_view);
        return view;
    }

    @Override
    public void execTask() {
        textView.setText("");
    }

    @Override
    public void stopTask() {
    }

    protected void appendLog(final String info){
        requireActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                textView.append(info+"\n");
            }
        });
        scrollView.postDelayed(new Runnable() {
            @Override
            public void run() {
                scrollView.fullScroll(ScrollView.FOCUS_DOWN);
            }
        }, 50);
    }

    protected void appendLogF(final String content){
            requireActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    textView.append(getSpanStr(content+"\n", Color.RED));

                }
            });
            scrollView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    scrollView.fullScroll(ScrollView.FOCUS_DOWN);
                }
            }, 50);

    }
}
